package com.qingk.easyjava.beanfactory;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

public class MyBeanFactory {
	private List<MyBeanDefinition> definitions;
	private Map<String, Object> singletonBeans;
	private Map<String, MyBeanDefinition> prototypeBeans;

	public MyBeanFactory() {
		definitions = new ArrayList<>();
		singletonBeans = new HashMap<>();
		prototypeBeans = new HashMap<>();

		parse();
		resolveBeanDefinitions();
	}
	
	//解析注解类
	private void parseAnnotation() {
		
	}
	
	// 解析配置文件
	private void parse() {
		SAXBuilder sb = new SAXBuilder();
		try {
			Document myDocument = sb.build("D:\\workSpaceForLHZ\\spring\\src\\main\\resources\\mybean.xml");
			Element rootElement = myDocument.getRootElement();
			List<Element> childList = rootElement.getChildren();
			for (Element e : childList) {
				if(e.getName().equals("bean")) {
				MyBeanDefinition md = new MyBeanDefinition();
				md.setId(e.getAttribute("id").getValue());
				md.setClassName(e.getAttributeValue("class"));
				String singleton = e.getAttributeValue("singleton");
				if(singleton != null) {
					md.setSingleton(Boolean.parseBoolean(singleton));
				}
				definitions.add(md);
				} else if(e.getName().equals("context")) {
					String className = e.getAttributeValue("base-package");
					try {
						Class clazz = Class.forName(className);
						Annotation[] as = clazz.getAnnotations();
						for(Annotation a : as) {
							System.out.println("1"+a);
						}
					} catch (ClassNotFoundException e1) {
						e1.printStackTrace();
					}
				}
			}
			
			
		} catch (JDOMException | IOException e) {
			e.printStackTrace();
		}
	}
	
	private void resolveBeanDefinitions() {
		for(MyBeanDefinition md : definitions) {
			if(md.isSingleton()) {
				singletonBeans.put(md.getId(), getInstance(md));
			} else {
				prototypeBeans.put(md.getId(), md);
			}
		}
	}

	//根据反射来创建实例
	private Object getInstance(MyBeanDefinition md) {
		String className = md.getClassName();
		Class clazz = null;
		Object object = null;
		try {
			clazz = Class.forName(className);
			object = clazz.newInstance();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return object;
	}

	public Object getBean(String beanName) {
		Object object = singletonBeans.get(beanName);
		
		return (object != null) ? object : getInstance(prototypeBeans.get(beanName));
	}

	public <T> T getBean(String beanName, Class<T> clazz) {
		Object object = getBean(beanName);
		if(object.getClass().equals(clazz)) {
			return (T)object;
		}
		
		throw new RuntimeException("没有这个类型");
	}

	public Class getType(String beanName) {
		return getBean(beanName).getClass();
	}

	public boolean isSingleton(String beanName) {
		return singletonBeans.get(beanName) != null;
	}

	public String[] getAliases(String beanName) {
		return null;
	}

	public boolean containsBean(String beanName) {
		return singletonBeans.get(beanName) != null || prototypeBeans.get(beanName) != null;
	}

}
